package cn.com.cfae.iras.doc.analyze.components.consistency;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zouxun on 2018/10/29
 */
@Component("U1015")
@Scope("prototype")
public class U1015 extends BaseComponent{
    private static Logger logger = LoggerFactory.getLogger(U1015.class);

    @Autowired
    U1004 u1004;

    @Override
    public List<ComponentResult> handlerM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        logger.info("调用U1015组件,处理BM表...................");
        List<ComponentResult> componentResults = ruleCheck();
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        logger.info("调用U1015组件,处理BDM表...................");
        List<ComponentResult> componentResults = ruleCheck();
        return componentResults;
    }

    private Map<String, List<Map<String, ExtractItem>>> getTable(Map<String, ExtractBizModel> extractBizModelMap,String tableName){
        logger.info("表名:{}",tableName);
        ExtractBizModel extractBizModel = extractBizModelMap.get(tableName);
        if(extractBizModel!=null){
            Map<String, List<Map<String, ExtractItem>>> map = extractBizModel.getTables2();
            return map;
        }else{
            return null;
        }
    }

    private Map<String,String> getData(List<Map<String, ExtractItem>> extractItemMapList,String item){
        Map<String,String> map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(extractItemMapList)){
            int count1 = 0;
            int count2 = 0;
            int count3 = 0;
            int count4 = 0;
            for(Map<String, ExtractItem> extractItemMap:extractItemMapList){
                if(MapUtils.isNotEmpty(extractItemMap)){
                    ExtractItem yybkExtractItem = extractItemMap.get("YWBK");
                    ExtractItem extractItem = extractItemMap.get(item);
                    if(yybkExtractItem!=null){
                        KVText yybkKvTextValue = yybkExtractItem.getValue();
                        if(yybkKvTextValue!=null){
                            String yybkText = yybkKvTextValue.getText();
                            if(StringUtils.isNotEmpty(yybkText)){
                                yybkText = yybkText.replaceAll("\\s{1,}","");
                                KVText kvTextValue = extractItem.getValue();
                                if(kvTextValue!=null){
                                    if(StringUtils.isNotEmpty(kvTextValue.getText())){
                                        map.put(yybkText,kvTextValue.getText());
                                    }else{
                                        map.put(yybkText,null);
                                    }
                                }else{
                                    map.put(yybkText,null);
                                }
                            }else{
                                count2++;
                            }
                        }else{
                            count3++;
                        }
                    }else{
                        count4++;
                    }
                }else{
                    count1++;
                }
            }
            if(count1==extractItemMapList.size()){
                return null;
            }
            if(count2==extractItemMapList.size()){
                return null;
            }
            if(count3==extractItemMapList.size()){
                return null;
            }
            if(count4==extractItemMapList.size()){
                return null;
            }
        }else{
            return null;
        }
        return map;
    }

    private String dealNumber(String text){
        logger.info("数值处理前：{}",text);
        if(StringUtils.isNotEmpty(text)){
            text=text.replaceAll("[%|\\s|,|，]{1,}","");
        }
        logger.info("数值处理后：{}",text);
        return text;
    }

    private Boolean symbolicJudgment(String text){
        String regex = "(——|\\-|--){1}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }

    private Boolean numberVerdict(String text){
        String regex = "-{0,1}\\d{1,}.{0,1}\\d{0,}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }

    private TextLayout getMaxMllTextLayout(Map<String, List<Map<String, ExtractItem>>> map){
        if(MapUtils.isNotEmpty(map)){
            Set<String> yearSet = map.keySet();
            List<Map<String, ExtractItem>> maxList = map.get(Collections.max(yearSet));

            if(CollectionUtils.isNotEmpty(maxList)){
                Map<String, ExtractItem> maxExtractItemMap = maxList.get(0);
                if(maxExtractItemMap!=null){
                    ExtractItem maxExtractItem = maxExtractItemMap.get("YWBK");
                    if(maxExtractItem!=null){
                        KVText kvText = maxExtractItem.getValue();
                        if(kvText!=null){
                            TextLayout textLayout = kvText.getTextLayout();
                            if(textLayout!=null){
                                return textLayout;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }


    private String getMllZbValue(List<Map<String, ExtractItem>> mllExtractItemList,String yysrYwbkText){
        if(CollectionUtils.isNotEmpty(mllExtractItemList)){
            for(Map<String, ExtractItem> mllExtractItemMap:mllExtractItemList) {
                ExtractItem mllYwbkExtractItem = mllExtractItemMap.get("YWBK");
                ExtractItem mllZbExtractItem = mllExtractItemMap.get("ZB");

                if(mllYwbkExtractItem!=null){
                    KVText mllYwbkKVTextValue = mllYwbkExtractItem.getValue();
                    if(mllYwbkKVTextValue!=null) {
                        String mllYWbkText = mllYwbkKVTextValue.getText();
                        if(StringUtils.isNotEmpty(mllYWbkText)) {
                            mllYWbkText = mllYWbkText.replaceAll("\\s{1,}","");
                            if(mllYWbkText.equals(yysrYwbkText)){
                                if(mllZbExtractItem!=null){
                                    KVText mllZbKVTextValue = mllZbExtractItem.getValue();
                                    if(mllZbKVTextValue!=null) {
                                        String mllZbText = mllZbKVTextValue.getText();
                                        if(StringUtils.isNotEmpty(mllZbText)) {
                                            return mllZbText;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

    private TextLayout getTextLayout(List<Map<String, ExtractItem>> mllExtractItemList,String yysrYwbkText){
        if(CollectionUtils.isNotEmpty(mllExtractItemList)){
            for(Map<String, ExtractItem> mllExtractItemMap:mllExtractItemList) {
                ExtractItem mllYwbkExtractItem = mllExtractItemMap.get("YWBK");

                if(mllYwbkExtractItem!=null){
                    KVText mllYwbkKVTextValue = mllYwbkExtractItem.getValue();
                    if(mllYwbkKVTextValue!=null) {
                        String mllYWbkText = mllYwbkKVTextValue.getText();
                        if(StringUtils.isNotEmpty(mllYWbkText)) {
                            mllYWbkText = mllYWbkText.replaceAll("\\s{1,}","");
                            if(mllYWbkText.equals(yysrYwbkText)){
                                KVText mllYwbkKVTextKey = mllYwbkExtractItem.getValue();
                                if(mllYwbkKVTextKey!=null){
                                    if(mllYwbkKVTextKey.getTextLayout()!=null){
                                        return mllYwbkKVTextKey.getTextLayout();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }


    private List<ComponentResult> ruleCheck(){
        List<ComponentResult> componentResults = new ArrayList<>();
        try{
            DocumentModel document = super.getExtractData();

            ChapterModel chapterModel = document.getChapterModel(getCatalogId());

            if(chapterModel!=null){
                Map<String, ExtractBizModel> extractBizModelMap = chapterModel.getExtractBizModelMap();

                if(MapUtils.isNotEmpty(extractBizModelMap)){
                    //营业收入
                    Map<String, List<Map<String, ExtractItem>>> yysrMap = getTable(extractBizModelMap,"YYSR");
                    //营业成本
                    Map<String, List<Map<String, ExtractItem>>> yycbMap = getTable(extractBizModelMap,"YYCB");
                    //毛利率
                    Map<String, List<Map<String, ExtractItem>>> mllMap = getTable(extractBizModelMap,"MLL");

                    if(MapUtils.isNotEmpty(yysrMap) && MapUtils.isNotEmpty(yycbMap)){
                        int blankCount = 0;
                        //不一致
                        int inconformityCount = 0;
                        //一致
                        int consistentCount = 0;
                        //未识别或无法确定
                        int unidentification=0;

                        Set<String> yysrYearSet = yysrMap.keySet();

                        for(String yysrYear:yysrYearSet) {
                            logger.info("获取年份：{}", yysrYear);
                            //营业收入
                            List<Map<String, ExtractItem>> yysrExtractItemList = yysrMap.get(yysrYear);

                            Map<String, String> yysrDataMap = getData(yysrExtractItemList, "YYSR");

                            //营业成本
                            List<Map<String, ExtractItem>> yycbExtractItemList = yycbMap.get(yysrYear);

                            Map<String, String> yycbDataMap = getData(yycbExtractItemList, "YYCB");

                            if (MapUtils.isNotEmpty(yysrDataMap) && MapUtils.isNotEmpty(yycbDataMap)) {

                                Set<String> yysrYWbkTextSet = yysrDataMap.keySet();

                                for (String yysrYWbkText : yysrYWbkTextSet) {
                                    logger.info("年份：{}，营业收入业务板块：{}", yysrYear, yysrYWbkText);

                                    String keyword = "年份：".concat(yysrYear.substring(0, 4)).concat("，业务：").concat(yysrYWbkText);

                                    ComponentResult componentResult = u1004.getComponentResult(getCatalogId(), getChapterId(), getRuleTitle(), keyword, "", true, getRegulationId(), chapterModel.getStartPage(), null, "03");

                                    TextLayout textLayout1 = getTextLayout(yysrExtractItemList, yysrYWbkText);
                                    List<ComponentResult.TextPosition> textPositionList1 = new ArrayList<>();
                                    if(textLayout1!=null){
                                        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                                        componentResult.setPageNum(textLayout1.getPageNumber());
                                        textPosition.setX(textLayout1.getLeft());
                                        textPosition.setY(textLayout1.getTop());
                                        textPosition.setContent(textLayout1.getText());
                                        textPositionList1.add(textPosition);
                                    }
                                    componentResult.setTextPosition(textPositionList1);

                                    //营业收入
                                    String yysrValue = yysrDataMap.get(yysrYWbkText);
                                    logger.info("营业收入数值：{}", yysrValue);

                                    //营业成本
                                    String yycbValue = yycbDataMap.get(yysrYWbkText);
                                    logger.info("营业成本数值：{}", yycbValue);


                                    if (StringUtils.isNotEmpty(yysrValue) && StringUtils.isNotEmpty(yycbValue)) {
                                        if (!symbolicJudgment(yysrValue) && !symbolicJudgment(yycbValue)) {
                                            yysrValue = dealNumber(yysrValue);
                                            yycbValue = dealNumber(yycbValue);

                                            if (numberVerdict(yysrValue) && numberVerdict(yycbValue)) {
                                                BigDecimal yysrDecimal = new BigDecimal(yysrValue);
                                                BigDecimal yycbDecimal = new BigDecimal(yycbValue);

                                                if (yysrDecimal.compareTo(BigDecimal.ZERO) != 0) {
                                                    //营业毛利率 =（营业收入－营业成本）/营业收入
                                                    BigDecimal grossProfit = (((yysrDecimal.subtract(yycbDecimal)).divide(yysrDecimal, 4, BigDecimal.ROUND_HALF_UP)).multiply(BigDecimal.valueOf(100)));
                                                    logger.info("系统计算的毛利率为：{}", grossProfit.stripTrailingZeros().toPlainString());

                                                    if(!yysrYWbkText.equals("合计")){
                                                        if(MapUtils.isNotEmpty(mllMap)){
                                                            //毛利率
                                                            List<Map<String, ExtractItem>> mllExtractItemList = mllMap.get(yysrYear);

                                                            String mllZbValue = getMllZbValue(mllExtractItemList, yysrYWbkText);
                                                            logger.info("业务：{}，对应毛利率数值：{}",yysrYWbkText,mllZbValue);

                                                            if(StringUtils.isNotEmpty(mllZbValue)){
                                                                TextLayout textLayout = getTextLayout(mllExtractItemList, yysrYWbkText);
                                                                List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                                                                if(textLayout!=null){
                                                                    ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                                                                    textPosition.setX(textLayout.getLeft());
                                                                    textPosition.setY(textLayout.getTop());
                                                                    textPosition.setContent(textLayout.getText());
                                                                    textPositionList.add(textPosition);
                                                                    componentResult.setPageNum(textLayout.getPageNumber());
                                                                }
                                                                componentResult.setTextPosition(textPositionList);

                                                                if(!symbolicJudgment(mllZbValue)){
                                                                    mllZbValue = dealNumber(mllZbValue);
                                                                    if(numberVerdict(mllZbValue)){
                                                                        BigDecimal mllDecimal = new BigDecimal(mllZbValue);

//                                                                        componentResult.setAnnotationFlag(false);

                                                                        if(mllDecimal.compareTo(grossProfit) == 0){
                                                                            logger.info("比较结果：一致");
                                                                            componentResult.setPromptMessage("一致");
                                                                            componentResult.setExist("01");
                                                                            componentResult.setAnnotationFlag(false);
                                                                            consistentCount++;
                                                                        }else{
                                                                            logger.info("比较结果：不一致");
                                                                            String promptMessage = "不一致".concat("，系统计算后的毛利率为：").concat(grossProfit.stripTrailingZeros().toPlainString()).concat("%");
                                                                            componentResult.setPromptMessage(promptMessage);
                                                                            componentResult.setExist("02");
                                                                            componentResult.setAnnotationFlag(true);
                                                                            inconformityCount++;
                                                                        }
                                                                        componentResults.add(componentResult);

                                                                    }else{
                                                                        componentResult.setPromptMessage("未识别毛利率，系统计算后的毛利率为：".concat(grossProfit.stripTrailingZeros().toPlainString()).concat("%").concat("，建议人工核对"));
                                                                        componentResults.add(componentResult);
                                                                        unidentification++;
                                                                    }

                                                                }else{
                                                                    componentResult.setPromptMessage("无法比较，毛利率未披露，系统计算后的毛利率为：".concat(grossProfit.stripTrailingZeros().toPlainString()).concat("%"));
                                                                    componentResults.add(componentResult);
                                                                    unidentification++;
                                                                }

                                                            }else{
                                                                componentResult.setPromptMessage("未识别毛利率数据表，系统计算的毛利率为：".concat(grossProfit.stripTrailingZeros().toPlainString()).concat("%"));
                                                                componentResults.add(componentResult);
                                                                unidentification++;
                                                            }

                                                        } else {
                                                            componentResult.setPromptMessage("未识别毛利率数据表，系统计算的毛利率为：".concat(grossProfit.stripTrailingZeros().toPlainString()).concat("%"));
                                                            componentResults.add(componentResult);
                                                            unidentification++;
                                                        }
                                                    }

                                                } else {
                                                    componentResult.setPromptMessage("营业收入数值为0，无法计算毛利率");
                                                    componentResults.add(componentResult);
                                                    unidentification++;
                                                }

                                            } else if (!numberVerdict(yysrValue) && numberVerdict(yycbValue)) {
                                                componentResult.setPromptMessage("未识别营业收入，已识别营业成本：".concat(yycbValue).concat("，建议人工核对"));
                                                componentResults.add(componentResult);
                                                unidentification++;
                                            } else if (numberVerdict(yysrValue) && !numberVerdict(yycbValue)) {
                                                componentResult.setPromptMessage("未识别营业成本，已识别营业收入：".concat(yysrValue).concat("，建议人工核对"));
                                                componentResults.add(componentResult);
                                                unidentification++;
                                            } else if (!numberVerdict(yysrValue) && !numberVerdict(yycbValue)) {
                                                componentResult.setPromptMessage("未识别营业收入和营业成本，建议人工核对");
                                                componentResults.add(componentResult);
                                                unidentification++;
                                            }
                                        } else if (symbolicJudgment(yysrValue) && !symbolicJudgment(yycbValue)) {
                                            componentResult.setPromptMessage("无法计算毛利率，未披露营业收入");
                                            componentResults.add(componentResult);
                                            unidentification++;
                                        } else if (!symbolicJudgment(yysrValue) && symbolicJudgment(yycbValue)) {
                                            componentResult.setPromptMessage("无法计算毛利率，未披露营业成本");
                                            componentResults.add(componentResult);
                                            unidentification++;
                                        } else if (symbolicJudgment(yysrValue) && symbolicJudgment(yycbValue)) {
                                            componentResult.setPromptMessage("无法计算毛利率，未披露营业收入和营业成本");
                                            componentResults.add(componentResult);
                                            unidentification++;
                                        }

                                    } else if (StringUtils.isEmpty(yysrValue) && StringUtils.isNotEmpty(yycbValue)) {
                                        componentResult.setPromptMessage("未识别营业收入，已识别营业成本：".concat(yycbValue).concat("，建议人工核对"));
                                        componentResults.add(componentResult);
                                        unidentification++;
                                    } else if (StringUtils.isNotEmpty(yysrValue) && StringUtils.isEmpty(yycbValue)) {
                                        componentResult.setPromptMessage("未识别营业成本，已识别营业收入：".concat(yysrValue).concat("，建议人工核对"));
                                        componentResults.add(componentResult);
                                        unidentification++;
                                    }
                                }

                            }else if(MapUtils.isNotEmpty(yysrDataMap) && MapUtils.isEmpty(yycbDataMap)){
                                Set<String> yysrYWbkTextSet = yysrDataMap.keySet();
                                for(String yysrYWbkText:yysrYWbkTextSet){
                                    String keyword = "年份：".concat(yysrYear.substring(0, 4)).concat("，业务：").concat(yysrYWbkText);
                                    ComponentResult componentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),keyword,"",true,getRegulationId(),chapterModel.getStartPage(),null,"03");

                                    String promptMessage = "已识别营业收入：".concat(yysrDataMap.get(yysrYWbkText)).concat("，未识别营业成本，建议人工核对");
                                    componentResult.setPromptMessage(promptMessage);

                                    TextLayout textLayout=null;
                                    if(MapUtils.isNotEmpty(mllMap)){
                                        //毛利率
                                        List<Map<String, ExtractItem>> mllExtractItemList = mllMap.get(yysrYear);
                                        textLayout = getTextLayout(mllExtractItemList, yysrYWbkText);
                                    }else{
                                        textLayout = getTextLayout(yysrExtractItemList, yysrYWbkText);
                                    }

                                    List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                                    if(textLayout!=null){
                                        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                                        textPosition.setX(textLayout.getLeft());
                                        textPosition.setY(textLayout.getTop());
                                        textPosition.setContent(textLayout.getText());
                                        componentResult.setPageNum(textLayout.getPageNumber());
                                        textPositionList.add(textPosition);
                                    }
                                    componentResult.setTextPosition(textPositionList);
                                    componentResults.add(componentResult);
                                }
                                return componentResults;
                            }else if(MapUtils.isEmpty(yysrDataMap) && MapUtils.isNotEmpty(yycbDataMap)){
                                Set<String> yycbYWbkTextSet = yycbDataMap.keySet();
                                for(String yycbYWbkText:yycbYWbkTextSet){
                                    String keyword = "年份：".concat(yysrYear.substring(0, 4)).concat("，业务：").concat(yycbYWbkText);

                                    ComponentResult componentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),keyword,"",true,getRegulationId(),chapterModel.getStartPage(),null,"03");

                                    String promptMessage = "已识别营业成本：".concat(yycbDataMap.get(yycbYWbkText)).concat("，未识别营业收入，建议人工核对");
                                    componentResult.setPromptMessage(promptMessage);

                                    TextLayout textLayout=null;
                                    if(MapUtils.isNotEmpty(mllMap)){
                                        //毛利率
                                        List<Map<String, ExtractItem>> mllExtractItemList = mllMap.get(yysrYear);
                                        textLayout = getTextLayout(mllExtractItemList,yycbYWbkText);
                                    }else{
                                        textLayout = getTextLayout(yysrExtractItemList,yycbYWbkText);
                                    }
                                    List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                                    if(textLayout!=null){
                                        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                                        textPosition.setX(textLayout.getLeft());
                                        textPosition.setY(textLayout.getTop());
                                        textPosition.setContent(textLayout.getText());
                                        textPositionList.add(textPosition);
                                        componentResult.setPageNum(textLayout.getPageNumber());
                                    }
                                    componentResult.setTextPosition(textPositionList);
                                    componentResults.add(componentResult);
                                }
                                return componentResults;
                            }else if(MapUtils.isEmpty(yysrDataMap) && MapUtils.isEmpty(yycbDataMap)){
                                blankCount++;
                            }
                        }

                        if(blankCount==yysrYearSet.size()){
                            ComponentResult componentResult = u1004.getComponentResult(getCatalogId(), getChapterId(), getRuleTitle(), "检查募集说明书中主营业务介绍部分中的“毛利率”和经测算后的结果值比是否一致。", "未识别营业收入和营业成本，建议人工核对", true, getRegulationId(), chapterModel.getStartPage(), null, "03");

                            List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                            ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                            TextLayout mllTextLayout = getMaxMllTextLayout(mllMap);
                            if(mllTextLayout!=null){
                                componentResult.setPageNum(mllTextLayout.getPageNumber());
                                textPosition.setX(mllTextLayout.getLeft());
                                textPosition.setY(mllTextLayout.getTop());
                                textPosition.setContent(mllTextLayout.getText());
                            }
                            textPositionList.add(textPosition);
                            componentResult.setTextPosition(textPositionList);
                            componentResults.add(componentResult);
                            return componentResults;

                        }else{
                            if(componentResults.size()>0){
                                if(inconformityCount>0){
                                    for(ComponentResult componentResult : componentResults) {
                                        componentResult.setExist("02");
                                    }
                                    return componentResults;
                                }else{
                                    if(unidentification>0){
                                        for(ComponentResult componentResult : componentResults) {
                                            componentResult.setExist("03");
                                        }
                                        return componentResults;
                                    }else{
                                        if(consistentCount>0){
                                            for(ComponentResult componentResult : componentResults) {
                                                componentResult.setExist("01");
                                                componentResult.setAnnotationFlag(false);
                                            }
                                            return componentResults;
                                        }
                                    }
                                }
                            }
                        }

                    }else if(MapUtils.isEmpty(yysrMap) && MapUtils.isNotEmpty(yycbMap)){
                        ComponentResult componentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),"检查募集说明书中主营业务介绍部分中的“毛利率”和经测算后的结果值比是否一致。","未识别营业收入，建议人工核对",true,getRegulationId(),chapterModel.getStartPage(),null,"03");

                        List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                        TextLayout mllTextLayout = getMaxMllTextLayout(mllMap);

                        if(mllTextLayout!=null){
                            textPosition.setX(mllTextLayout.getLeft());
                            textPosition.setY(mllTextLayout.getTop());
                            textPosition.setContent(mllTextLayout.getText());
                            componentResult.setPageNum(mllTextLayout.getPageNumber());
                        }else{
                            TextLayout yycbTextLayout = getMaxMllTextLayout(yycbMap);
                            if(yycbTextLayout!=null){
                                textPosition.setX(yycbTextLayout.getLeft());
                                textPosition.setY(yycbTextLayout.getTop());
                                textPosition.setContent(yycbTextLayout.getText());
                                componentResult.setPageNum(yycbTextLayout.getPageNumber());
                            }
                        }
                        textPositionList.add(textPosition);
                        componentResult.setTextPosition(textPositionList);
                        componentResults.add(componentResult);
                        return componentResults;
                    }else if(MapUtils.isNotEmpty(yysrMap) && MapUtils.isEmpty(yycbMap)){
                        ComponentResult componentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),"检查募集说明书中主营业务介绍部分中的“毛利率”和经测算后的结果值比是否一致。","未识别营业成本，建议人工核对",true,getRegulationId(),chapterModel.getStartPage(),null,"03");

                        List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                        TextLayout mllTextLayout = getMaxMllTextLayout(mllMap);
                        if(mllTextLayout!=null){
                            textPosition.setX(mllTextLayout.getLeft());
                            textPosition.setY(mllTextLayout.getTop());
                            textPosition.setContent(mllTextLayout.getText());
                            componentResult.setPageNum(mllTextLayout.getPageNumber());
                        }else{
                            TextLayout yysrTextLayout = getMaxMllTextLayout(yysrMap);
                            if(yysrTextLayout!=null){
                                textPosition.setX(yysrTextLayout.getLeft());
                                textPosition.setY(yysrTextLayout.getTop());
                                textPosition.setContent(yysrTextLayout.getText());
                                componentResult.setPageNum(yysrTextLayout.getPageNumber());
                            }
                        }
                        textPositionList.add(textPosition);
                        componentResult.setTextPosition(textPositionList);
                        componentResults.add(componentResult);
                        return componentResults;
                    }else if(MapUtils.isEmpty(yysrMap) && MapUtils.isEmpty(yycbMap)){
                        ComponentResult componentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),"检查募集说明书中主营业务介绍部分中的“毛利率”和经测算后的结果值比是否一致。","未识别营业收入和营业成本，建议人工核对",true,getRegulationId(),chapterModel.getStartPage(),null,"03");
                        List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                        ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                        TextLayout mllTextLayout = getMaxMllTextLayout(mllMap);
                        if(mllTextLayout!=null){
                            textPosition.setX(mllTextLayout.getLeft());
                            textPosition.setY(mllTextLayout.getTop());
                            textPosition.setContent(mllTextLayout.getText());
                            componentResult.setPageNum(mllTextLayout.getPageNumber());
                        }
                        textPositionList.add(textPosition);
                        componentResult.setTextPosition(textPositionList);
                        componentResults.add(componentResult);
                        return componentResults;
                    }

                }else{
                    ComponentResult componentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),"检查募集说明书中主营业务介绍部分中的“毛利率”和经测算后的结果值比是否一致","",true,getRegulationId(),chapterModel.getStartPage(),null,"03");
                    componentResult.setPromptMessage("未识别".concat(getCatalogId()).concat(",建议人工核对"));
                    componentResults.add(componentResult);
                    return componentResults;
                }

            }else{
                ComponentResult componentResult = u1004.getComponentResult(getCatalogId(),getChapterId(),getRuleTitle(),"检查募集说明书中主营业务介绍部分中的“毛利率”和经测算后的结果值比是否一致","",true,getRegulationId(),0,null,"03");
                componentResult.setPromptMessage("未识别".concat(getCatalogId()).concat(",建议人工核对"));
                componentResults.add(componentResult);
                return componentResults;
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return componentResults;
    }

}